<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
class GoodsZhudaController extends Controller
{
    public function add($id)
    {
    	if($id == 1)
    	{
		    $data = DB::table('goods') ->select() -> get();
    		return view('admin.zhuda.rexiao',['title'=>'热销商品','data'=>$data]);
    	}

        if($id == 2)
        {
            $data = DB::table('goods') ->select() -> get();
            return view('admin.zhuda.xinpin',['title'=>'新品商品','data'=>$data]);
        }

    	if($id == 3)
    	{
    		$data = DB::table('goods') ->select() -> get();
            return view('admin.zhuda.jingpin',['title'=>'精品商品','data'=>$data]);
    	}

    }


    public function doadd(Request $request)
    {
    	
    	$this -> validate($request,[
            'pic' => 'image|max:2048',
            ],[
            'pic.image' => '你必须传一张图片',
            'pic.max' => '最大2M' 
            ]);
    	$data = $request -> except('_token');

    	if($request -> hasFile('gpic'))
        {

            if($request -> file('gpic') -> isValid())
            {
                //处理头像
                //获取文件后缀

                $extension = $request -> file('gpic') -> getClientOriginalExtension();

                $filename = mt_rand(100000,999999999).'.'.$extension;

                $dir = './uploads/goodszhuda/';

         
                $request -> file('gpic') -> move($dir, $filename);
                $data['gpic'] = '/uploads/goodszhuda/'.$filename;

            }else
            {
                $data['gpic'] = '/uploads/goodszhuda/default.jpg';
            }

        } 

        $res = DB::table('goodszhuda') ->insert([$data]); 
        if($res)
         {
          return back() -> with(['info' => '添加到主打栏成功']);
         }else
         {
          return back() -> with(['info' => '添加失败']);
         }
		
    }



    public function index(Request $request)
    {
        $num = $request -> input('num',10);
        $keywords = $request -> input('keywords','');
        $data = DB::table('goodszhuda') -> where('leixing','like','%'.$keywords.'%') -> paginate($num) ;
        foreach($data as $row){
            $row -> gid = DB::table('goods') -> where('gid',$row -> gid) -> first() -> gname;
        }
        

        return view('admin.zhuda.indexZhuda',['data' => $data ,'request' => $request -> all(), 'title' => '主打商品列表']);
    }

    public function ajax(Request $request)
    {
        $id = $request -> id;
        $price = $request -> price;
        $res = DB::table('goodszhuda') -> where('id',$id) -> update(['price'=>$price]);
       if($res)
       {
        return 1;
       }else
       {
        return 0;
       }
    }

    public function deletezhuda($id)
    {
        $res = DB::table('goodszhuda') -> where('id',$id) -> delete();
        if($res)
         {
          return back() -> with(['info' => '移出成功']);
         }else
         {
          return back() -> with(['info' => '移出失败']);
         }
    }



}
